package com.car.order.aop;


import com.car.order.annotation.ServeOrderLogAnnotation;
import com.car.order.dto.goodOrderInfo.PayOrderForm;
import com.car.order.dto.serviceOrderInfo.ServiceOrderInfoCreatDto;
import com.car.order.entity.ServeOrderLog;
import com.car.order.entity.form.serveOrderInfo.AddReviewForm;
import com.car.order.mapper.ServeOrderLogMapper;
import com.car.order.util.GoodsOrderDescConstants;
import com.car.order.util.ServeOrderDescConstants;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


@Component
@Aspect
@Slf4j
public class ServeOrderLogAop {
    @Resource
    private ServeOrderLogMapper serveOrderLogMapper;
    @Around("@annotation(com.car.order.annotation.ServeOrderLogAnnotation)")
    public Object log(ProceedingJoinPoint pjp) throws Throwable {
        //获取注解
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        ServeOrderLogAnnotation declaredAnnotation = signature.getMethod().getDeclaredAnnotation(ServeOrderLogAnnotation.class);
        /* 获取方法入参 */
        Object[] args = pjp.getArgs();
        // 获取注解上的，订单操作描述
        String desc = declaredAnnotation.desc();
        ServeOrderLog serveOrderLog = new ServeOrderLog();
        Object proceed = pjp.proceed();
        serveOrderLog.setLogDesc(desc);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        serveOrderLog.setLogTime(LocalDateTime.now().format(formatter));
        //如果是用户新增订单
        if (desc.equals(ServeOrderDescConstants.USER_CREATE)) {
            ServiceOrderInfoCreatDto proceed1 = (ServiceOrderInfoCreatDto) proceed;
            serveOrderLog.setServeOrderId(proceed1.getServeOrderId());
        }
        //如果是用户支付订单
        if (desc.equals(ServeOrderDescConstants.USER_PAID)) {
            for (Object arg : args) {
                if (arg instanceof PayOrderForm) {
                    PayOrderForm form = (PayOrderForm) arg;
                    serveOrderLog.setServeOrderId(form.getOrderId());
                }
            }
        }
        //如果是用户退款
        if (desc.equals(ServeOrderDescConstants.USER_TUI_KUAN)) {
            for (Object arg : args) {
                if (arg instanceof String) {
                    String id = (String) arg;
                    serveOrderLog.setServeOrderId(id);
                }
            }
        }
        //评价
        if (desc.equals(ServeOrderDescConstants.USER_EVALUATE)) {
            for (Object arg : args) {
                if (arg instanceof AddReviewForm) {
                    AddReviewForm form = (AddReviewForm) arg;
                    serveOrderLog.setServeOrderId(form.getOrderId());
                }
            }
        }
        serveOrderLogMapper.insert(serveOrderLog);
        return proceed;
    }
}
